/*
 * 用法
 *
 * Get:
 *   mi.xxservice.callGet("/methodname/{param1}/{param2}",{param1:1,param2:2},callback) or   mi.xxservice.callGet("/methodname?p={param1}&p2={param2}",{param1:1,param2:2},callback)
 * Post
 *   mi.xxservice.callPost("/methodname",{param1:1,param2:1},callback);
 *
 */
var mi = {};
; (function ($) {
    //封装ajax请求
    var jsonProxy = function (serviceUrl) {
        this.callGet = function (url, params, suc, err) {
            /// <summary>封装ajax请求.</summary>
            /// <param name="url" type="String">服务的url [/user/{param2}/{param1}]</param>
            /// <param name="data" type="Object"></param>
            /// <param name="suc" type="Function">ajax请求成功后执行的回调函数.</param>
            /// <param name="err" type="Function">ajax请求失败后执行的回调函数.</param>
            var getUrl = url;

            var reg = /\{(.*?)\}/ig;
            var m = getUrl.match(reg);
            if (m) {
                m = Array.prototype.slice.call(m)
                for (var i in m) {
                    getUrl = getUrl.replace(m[i], params[m[i].toString().replace("{", "").replace("}", "")]);
                }
            }
            $.ajax({
                url: serviceUrl + getUrl,
                type: 'GET',
                cache: false,
                dataType: 'json',
                contentType: "application/json",
                error: function (obj) {
                    //ajax request error
                    //console.log("request error", obj);
                    if ($.isFunction(err)) {
                        var result = {};
                        result.d = obj;
                        err(result);
                    }
                },
                success: function (obj) {
                    //ajax request success
                    if ($.isFunction(suc)) {
                        var result = {};
                        result = obj;

                        suc(result);
                    }
                }
            })
        }

        this.callDelete = function (url, params, suc, err) {
            /// <summary>封装ajax请求.</summary>
            /// <param name="url" type="String">服务的url [/user/{param2}/{param1}]</param>
            /// <param name="data" type="Object"></param>
            /// <param name="suc" type="Function">ajax请求成功后执行的回调函数.</param>
            /// <param name="err" type="Function">ajax请求失败后执行的回调函数.</param>
            var getUrl = url;

            var reg = /\{(.*?)\}/ig;
            var m = getUrl.match(reg);
            if (m) {
                m = Array.prototype.slice.call(m)
                for (var i in m) {
                    getUrl = getUrl.replace(m[i], params[m[i].toString().replace("{", "").replace("}", "")]);
                }
            }
            $.ajax({
                url: serviceUrl + getUrl,
                type: 'DELETE',
                cache: false,
                dataType: 'json',
                contentType: "application/json",
                error: function (obj) {
                    //ajax request error
                    //console.log("request error", obj);
                    if ($.isFunction(err)) {
                        var result = {};
                        result.d = obj;
                        err(result);
                    }
                },
                success: function (obj) {
                    //ajax request success
                    if ($.isFunction(suc)) {
                        var result = {};
                        result = obj;

                        suc(result);
                    }
                }
            })
        }

        this.callPost = function (url, data, suc, err) {
            /// <summary>封装ajax请求.</summary>
            /// <param name="url" type="String">服务的url</param>
            /// <param name="data" type="Object">post data</param>
            /// <param name="suc" type="Function">ajax请求成功后执行的回调函数.</param>
            /// <param name="err" type="Function">ajax请求失败后执行的回调函数.</param>
            var getUrl = url;
            var d = JSON.stringify(data);

            $.ajax({
                url: serviceUrl + getUrl,
                type: 'POST',
                cache: false,
                data: d,
                dataType: 'json',
                contentType: "application/json",
                error: function (obj) {
                    //ajax request error
                    if ($.isFunction(err)) {
                        var result = {};
                        result = obj;
                        err(result);
                    }
                },
                success: function (obj) {
                    //ajax request success
                    if ($.isFunction(suc)) {
                        var result = {};
                        result = obj;

                        suc(result);
                    }
                }
            })
        }

        this.callPut = function (url, data, suc, err) {
            /// <summary>封装ajax请求.</summary>
            /// <param name="url" type="String">服务的url</param>
            /// <param name="data" type="Object">post data</param>
            /// <param name="suc" type="Function">ajax请求成功后执行的回调函数.</param>
            /// <param name="err" type="Function">ajax请求失败后执行的回调函数.</param>
            var getUrl = url;
            var d = JSON.stringify(data);

            $.ajax({
                url: serviceUrl + getUrl,
                type: 'PUT',
                cache: false,
                data: d,
                dataType: 'json',
                contentType: "application/json",
                error: function (obj) {
                    //ajax request error
                    if ($.isFunction(err)) {
                        var result = {};
                        result = obj;
                        err(result);
                    }
                },
                success: function (obj) {
                    //ajax request success
                    if ($.isFunction(suc)) {
                        var result = {};
                        result = obj;

                        suc(result);
                    }
                }
            })
        }
    };
    mi.apiService = new jsonProxy("api/kings");
    mi.productService = new jsonProxy("/products");
    //mi.eventService = new jsonProxy("/events");
    //mi.profileService = new jsonProxy("/profile");
    //mi.worksService = new jsonProxy("/works");
    //mi.userService = new jsonProxy("/user");
    //mi.groupService = new jsonProxy("/club");
    //api end
})(jQuery);